<template>
  <j-modal
    :title="title"
    :width="1200"
    :visible="visible"
    :maskClosable="false"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    :ok-button-props="{ props: { disabled: disableSubmit } }"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <!-- 主表单区域 -->
      <a-form :form="form">
        <a-row>
          <a-col :span="12">
            <a-form-item label="模版名" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input
                v-decorator="['name', validatorRules.name]"
                :disabled="disableSubmit"
                placeholder="请输入模版名"
              ></a-input>
            </a-form-item>
          </a-col>
          <!--<a-col :span="12">-->
            <!--<a-form-item label="类型" :labelCol="labelCol" :wrapperCol="wrapperCol">-->
              <!--<j-dict-select-tag-->
                <!--type="list"-->
                <!--@change="selectType"-->
                <!--:disabled="disableSubmit"-->
                <!--v-decorator="['typeId', validatorRules.typeId]"-->
                <!--:trigger-change="true"-->
                <!--dictCode="DataDictionary,description,dictionaryCode,parentId = root&dictionaryTypeId = AgreementType"-->
                <!--placeholder="请选择类型"-->
              <!--/>-->
            <!--</a-form-item>-->
          <!--</a-col>-->

          <a-col :span="12" v-if="specificList.length != 0">
            <a-form-item label="具体合同类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-select
                v-decorator="['specificTypeId']"
                :disabled="disableSubmit"
                style="width: 100%"
                placeholder="请选择具体合同类型"
              >
                <a-select-option v-for="specificId in specificList" :key="specificId.dictionaryId">
                  {{ specificId.description }}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>

          <a-col :span="12">
            <a-form-item label="签章关键字" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-select
                mode="tags"
                style="width: 100%"
                :token-separators="[',']"
                :disabled="disableSubmit"
                v-decorator="['keywords', validatorRules.keywords]"
                placeholder="请输入[盖章,签约日期]关键字,以英文逗号间隔"
              >
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['description']" :disabled="disableSubmit" placeholder="请输入备注"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="文件路径名" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-upload
                v-decorator="['uploadPath', validatorRules.uploadPath]"
                :disabled="disableSubmit"
                :trigger-change="true"
                :isMultiple="false"
              ></j-upload>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>

      <!-- 子表单区域 -->
      <a-tabs v-model="activeKey" @change="handleChangeTabs">
        <a-tab-pane tab="合同模板明细项表" :key="refKeys[0]" :forceRender="true">
          <j-editable-table
            :disabled="disableSubmit"
            :ref="refKeys[0]"
            :tableName="refKeys[0]"
            :loading="clmTemplateItemTable.loading"
            :columns="clmTemplateItemTable.columns"
            :dataSource="clmTemplateItemTable.dataSource"
            :maxHeight="300"
            :rowNumber="true"
            :rowSelection="true"
            :actionButton="!disableSubmit"
          />
        </a-tab-pane>
      </a-tabs>
    </a-spin>
  </j-modal>
</template>

<script>
import pick from 'lodash.pick'
import { FormTypes, getRefPromise } from '@/utils/JEditableTableUtil'
import { JEditableTableMixin } from '@/mixins/JEditableTableMixin'
import { validateDuplicateValue } from '@/utils/util'
import JUpload from '@/components/jeecg/JUpload'
import JDictSelectTag from '@/components/dict/JDictSelectTag'
import { httpAction, getAction } from '@/api/manage'
export default {
  name: 'ClmTemplateModal',
  mixins: [JEditableTableMixin],
  components: {
    JUpload,
    JDictSelectTag,
  },
  data() {
    return {
      disableSubmit: false,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 6 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      labelCol2: {
        xs: { span: 24 },
        sm: { span: 3 },
      },
      wrapperCol2: {
        xs: { span: 24 },
        sm: { span: 20 },
      },
      // 新增时子表默认添加几行空数据
      addDefaultRowNum: 1,
      validatorRules: {
        name: {
          rules: [{ required: true, message: '请输入模版名!' }],
        },
//        typeId: {
//          rules: [{ required: true, message: '请输入类型!' }],
//        },
        keywords: {
          rules: [{ required: true, message: '请输入盖章关键字!' }],
        },
        uploadPath: {
          rules: [{ required: true, message: '请上次附件!' }],
        },
      },
      refKeys: ['clmTemplateItem'],
      tableKeys: ['clmTemplateItem'],
      activeKey: 'clmTemplateItem',
      // 合同模板明细项表
      clmTemplateItemTable: {
        loading: false,
        dataSource: [],
        columns: [
          {
            title: '字段名',
            key: 'name',
            type: FormTypes.input,
            width: '200px',
            placeholder: '请输入${title}',
            defaultValue: '',
          },
          {
            title: '类型',
            key: 'fieldTypeId',
            type: FormTypes.select,
            dictCode: 'DataDictionary,description,dictionaryCode, dictionaryTypeId = SELECT_TYPE',
            width: '200px',
            placeholder: '请输入${title}',
            defaultValue: '',
            validateRules: [{ required: true, message: '${title}不能为空' }],
          },
          {
            title: '默认值',
            key: 'itemDefaultValue',
            type: FormTypes.input,
            width: '200px',
            placeholder: '请输入${title}',
            defaultValue: '',
          },
          {
            title: '表名',
            key: 'entityId',
            type: FormTypes.input,
            width: '200px',
            placeholder: '请输入${title}',
            defaultValue: '',
            readonly: false
          },
          {
            title: '字段',
            key: 'keyId',
            type: FormTypes.input,
            width: '200px',
            placeholder: '请输入${title}',
            defaultValue: '',
            readonly: false
          },
          {
            title: '数据字典类型',
            key: 'dictionaryTypeId',
            type: FormTypes.input,
            width: '200px',
            placeholder: '请输入${title}',
            defaultValue: '',
            readonly: false
          },
          {
            title: '排序',
            key: 'sequenceNum',
            type: FormTypes.input,
            width: '200px',
            placeholder: '请输入${title}',
            defaultValue: '',
          },
        ],
      },
      specificList: [],
      url: {
        add: '/online/special/api/createClmTemplate',
        edit: '/online/special/api/updateClmTemplate',
        selectDataDictionary: '/online/api/selectDataDictionary',
        clmTemplateItem: {
          list: '/online/api/listClmTemplateItemByClmTemplateId',
          deleteBatch: '/online/api/deleteClmTemplateItemByClmTemplateBatch',
        },
      },
    }
  },
  methods: {
    getAllTable() {
      let values = this.tableKeys.map((key) => getRefPromise(this, key))
      return Promise.all(values)
    },
    /** 调用完edit()方法之后会自动调用此方法 */
    editAfter(record) {
      if (this.model.keywords) {
        this.model.keywords = this.model.keywords.replace('[', '')
        this.model.keywords = this.model.keywords.replace(']', '')
        this.model.keywords = this.model.keywords.split(',')
      }
      console.log(this.model.keywords);
      let typeId = this.$route.params.typeId;
      this.selectType(typeId);
      this.model.typeId = typeId;
      let fieldval = pick(
        this.model,
        'name',
        'specificTypeId',
        'uploadPath',
        'description',
        'createDate',
        'createBy',
        'keywords'
      )

      this.$nextTick(() => {
        this.form.setFieldsValue(fieldval)
      })
      // 加载子表数据
      if (this.model.id) {
        let params = { id: this.model.id }
        this.requestSubTableData(this.url.clmTemplateItem.list, params, this.clmTemplateItemTable)
      }
    },
    /** 整理成formData */
    classifyIntoFormData(allValues) {
      let main = Object.assign(this.model, allValues.formValue)
      return {
        ...main, // 展开
        ClmTemplateItem: allValues.tablesValue[0].values,
      }
      console.log(allValues)
    },
    validateError(msg) {
      if (msg) {
        this.isHandleOk = false
      }
      this.$message.error(msg)
    },
    popupCallback(row) {
      this.form.setFieldsValue(
        pick(row, 'name','specificTypeId', 'uploadPath', 'description', 'createDate', 'createBy', 'keywords')
      )
    },
    selectType(e) {
      if (e == 'labor') {
        this.fromCompanyTypeId = 'GROUP'
        this.toCompanyTypeId = 'PERSON'
      } else {
        this.fromCompanyTypeId = 'GROUP'
        this.toCompanyTypeId = 'GROUP'
      }
      console.log(e);
      getAction(this.url.selectDataDictionary, { parentCode: e, dictionaryTypeId: 'AgreementType' }).then((res) => {
        if (res.success) {
          this.specificList = res.result.dictionaryList
        }
      })
    },
  },
}
</script>

<style scoped>
</style>
